package interactiond

import (
	"time"

	"github.com/globalsign/mgo"
	"github.com/globalsign/mgo/bson"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/common"
	cinteractiond "gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/interactiond"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/repo/mgorepo/baser"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/ddd"
)

type replayRepo struct {
	baser.BaseRepo
}

func (r replayRepo) Replay(ctx ddd.Context, path cinteractiond.ReplayPath, desc cinteractiond.ReplayDesc) (id string, err error) {
	uid := ctx.UID()
	now := time.Now()
	c := collReplay{
		CreTime:  now,
		Creator:  uid,
		ReplayID: bson.NewObjectId().Hex(),
	}
	c.FullDesc(desc)
	c.FullPath(path)
	return c.ReplayID, r.DBInsert(ctx, c)
}
func (r replayRepo) GetReplay(ctx ddd.Context, id string) (*cinteractiond.ReplayAggregate, error) {
	a, err := r.GetByCollAggregate(ctx, &collReplay{ReplayID: id})
	if err != nil || a == nil {
		if err == mgo.ErrNotFound {
			return nil, nil
		}
		return nil, err
	}
	return a.(*cinteractiond.ReplayAggregate), nil
}
func (r replayRepo) Delete(ctx ddd.Context, id string) error {
	update := bson.M{
		"status": cinteractiond.RStatusDel,
	}
	return r.DBUpdateID(ctx, collNameReplay, id, update)
}
func (r replayRepo) CheckOk(ctx ddd.Context, id string, rchk cinteractiond.ReplayCheck) error {

	update := bson.M{
		"checker":   rchk.Checker,
		"chkTime":   time.Now(),
		"chkContet": rchk.ChkContet,
		"chkResult": true,
	}
	return r.DBUpdateCollAggregate(ctx, &collReplay{ReplayID: id}, update)
}
func (r replayRepo) CheckNo(ctx ddd.Context, id string, rchk cinteractiond.ReplayCheck) error {
	update := bson.M{
		"checker":   rchk.Checker,
		"chkTime":   time.Now(),
		"chkContet": rchk.ChkContet,
		"chkResult": false,
	}
	return r.DBUpdateCollAggregate(ctx, &collReplay{ReplayID: id}, update)
}
func (r replayRepo) Destroy(ctx ddd.Context, id string) error {
	return r.DestroyByCollAggregate(ctx, &collReplay{ReplayID: id})
}
func (r replayRepo) QueryChildrenPage(ctx ddd.Context, id string, pi common.PageInfo) (pr common.PageResult, result []cinteractiond.ReplayAggregate, err error) {
	query := bson.M{
		"parentID": id,
	}
	return r.queryReplayPage(ctx, query, pi)
}
func (r replayRepo) QueryGroupReplayPage(ctx ddd.Context, igid string, s []cinteractiond.RStatus, pi common.PageInfo) (pr common.PageResult, result []cinteractiond.ReplayAggregate, err error) {
	query := bson.M{
		"igid":     igid,
		"parentID": "",
	}
	if len(s) > 0 {
		query["status"] = bson.M{"$in": s}
	}
	return r.queryReplayPage(ctx, query, pi)
}
func (r replayRepo) QueryGroupHostReplayPage(ctx ddd.Context, igid string, num int) (result []cinteractiond.ReplayAggregate, err error) {
	query := bson.M{
		"igid":      igid,
		"parentID":  "",
		"status":    cinteractiond.RStatusNormal,
		"replayNum": bson.M{"$gt": 0},
	}
	var cs []collReplay

	err = r.DBExecSync(ctx, collNameReplay, func(c *mgo.Collection) error {
		err := c.Find(query).Sort("-replayNum", "-_id").Limit(num).All(&cs)
		ctx.Logger().Debugf("QueryGroupHostReplayPage all %s <find:%v>  <select:%v> <sort:-replayNum, -_id> <limit:%d>", collNameReplay, query, num)
		return err
	})
	//_, err = r.DBQueryPage(ctx, collNameReplay, pi, "replayNum", query, nil, []string{"-_id"}, &cs)
	if len(cs) == 0 || err != nil {
		if err == mgo.ErrNotFound {
			err = nil
		}
		return
	}
	for _, c := range cs {
		result = append(result, *c.ToReplayAggregate())
	}
	return
}
func (r replayRepo) queryReplayPage(ctx ddd.Context, query bson.M, pi common.PageInfo) (pr common.PageResult, result []cinteractiond.ReplayAggregate, err error) {
	var cs []collReplay
	pr, err = r.DBQueryPage(ctx, collNameReplay, pi, "_id", query, nil, nil, &cs)
	if len(cs) == 0 || err != nil {
		if err == mgo.ErrNotFound {
			err = nil
		}
		return
	}
	for _, c := range cs {
		result = append(result, *c.ToReplayAggregate())
	}
	return
}
func (r replayRepo) AddReplayNum(ctx ddd.Context, id string, num int) error {
	return r.DBInc(ctx, collNameReplay, bson.M{"_id": id}, bson.M{"replayNum": num})
}
func (r replayRepo) AddLikeNum(ctx ddd.Context, id string, num int) error {
	return r.DBInc(ctx, collNameReplay, bson.M{"_id": id}, bson.M{"likeNum": num})
}
